﻿using Core.SimpleTemp.Api.Api.Sys;
using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Mvc.Api.Sys;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.Authorization.Help;
using Core.Toolkit.Common;
using Core.Toolkit.Core.Api;
using Core.Toolkit.DbInitializer;
using Core.Toolkit.Encrypt;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
#pragma warning disable CS1591
namespace Core.SimpleTemp.Mvc
{
    public class DBInitializer
    {
        private static void Initialize(CoreDBContext context, WebAppOptions _webAppOptions)
        {
            context.Database.EnsureCreated();
            //检查是否需要初始化数据
            if (context.Set<SysUser>().Any())
            {
                return;
            }
            //总部工厂ID
            var headofficeId = Guid.NewGuid();
            //铜陵
            var tlFactoryId = Guid.NewGuid();
            //山东
            var sdFactoryId = Guid.NewGuid();
            //华阳
            var hyFactoryId = Guid.NewGuid();

            #region 增加部门-职务-部门职务

            var newDepartmentId = Guid.NewGuid();
            //增加一个部门
            context.Set<SysDepartment>().Add(new SysDepartment
            {
                Id = newDepartmentId,
                Name = "总部",
                Code = "001",
                Sort = 1,
                CreateFactory = tlFactoryId
            });
            var zqNewDepartmentId = Guid.NewGuid();
            var 部门职务关系表id1 = Guid.NewGuid();
            var 部门职务关系表id2 = Guid.NewGuid();
            context.SysDepartment.Add(new SysDepartment
            {
                Id = zqNewDepartmentId,
                Code = "001001",
                Name = "研发部",
                ParentId = newDepartmentId,
                Sort = 1,
                CreateFactory = tlFactoryId
            });
            context.SysDepartment.Add(new SysDepartment
            {
                Code = "001002",
                Name = "市场部",
                ParentId = newDepartmentId,
                Sort = 2,
                CreateFactory = tlFactoryId,
                SysDepartmentDuties = new List<SysDepartmentDuty>() {
                      new SysDepartmentDuty{ Id=部门职务关系表id1, SysDuty=new SysDuty {
                          Name ="科长",
                          Position=1,
                          CreateFactory=tlFactoryId
                      } },
                      new SysDepartmentDuty{  SysDuty=new SysDuty {
                          Name ="班长",
                          Position=2,
                          CreateFactory=tlFactoryId

                      } },
                }
            });
            context.SysDepartment.Add(new SysDepartment
            {
                Code = "002",
                Name = "事业部",
                Sort = 2,
                CreateFactory = tlFactoryId,
                SysDepartmentDuties = new List<SysDepartmentDuty>() {
                      new SysDepartmentDuty{Id=部门职务关系表id2,  SysDuty=new SysDuty {
                          Name ="部长",
                          Position=3,
                          CreateFactory=tlFactoryId
                      } },
                      new SysDepartmentDuty{  SysDuty=new SysDuty {
                          Name ="组长",
                          Position=4,
                          CreateFactory=tlFactoryId
                      } },
                }
            });
            context.SysDepartment.Add(new SysDepartment
            {
                Code = "001001001",
                Name = "政企研发部",
                Sort = 1,
                ParentId = zqNewDepartmentId,
                CreateFactory = tlFactoryId
            });
            context.SaveChanges();
            var dep = context.Set<SysDepartment>().FirstOrDefault();
            #endregion
            //添加系统默认管理员
            var md5Password = MD5Helper.MD5(_webAppOptions.InitialPassword);
            var adminUser = context.Set<SysUser>().Add(new SysUser
            {
                LoginName = _webAppOptions.AdminLoginName,
                Password = md5Password,
                LastUpdate = DateTime.Now,
                Name = _webAppOptions.AdminLoginName,
                SysUserInfor = new SysUserInfor() { Name = "admin", Sex = "0", WorkCompany = headofficeId },

                SysUserDuties = new List<SysUserDuty>()
                {
                    new SysUserDuty
                    {
                       SysDepartmentDutyId=部门职务关系表id1
                    },
                    new SysUserDuty
                    {
                       SysDepartmentDutyId=部门职务关系表id2
                    }
                }
            });
            context.Set<SysUser>().Add(new SysUser { LoginName = "admin2", Name = "admin2", Password = md5Password, LastUpdate = DateTime.Now, SysUserInfor = new SysUserInfor() { Name = "admin2", Sex = "1", WorkCompany = headofficeId } });


            context.Set<SysUser>().Add(new SysUser { LoginName = "admintl", Name = "admin2", Password = md5Password, LastUpdate = DateTime.Now, SysUserInfor = new SysUserInfor() { Name = "admintl", Sex = "1", WorkCompany = tlFactoryId } });
            context.Set<SysUser>().Add(new SysUser { LoginName = "adminsd", Name = "admin2", Password = md5Password, LastUpdate = DateTime.Now, SysUserInfor = new SysUserInfor() { Name = "adminsd", Sex = "1", WorkCompany = sdFactoryId } });
            context.Set<SysUser>().Add(new SysUser { LoginName = "adminhy", Name = "admin2", Password = md5Password, LastUpdate = DateTime.Now, SysUserInfor = new SysUserInfor() { Name = "adminhy", Sex = "1", WorkCompany = hyFactoryId } });


            #region 增加四个基本功能菜单
            Guid baseSysMenuId = Guid.NewGuid();
            context.Set<SysMenu>().AddRange(
            //    new SysMenu
            //{
            //    Name = "组织机构管理",
            //    Code = "Department_Index",
            //    SerialNumber = 0,
            //    Icon = "fa fa-link",
            //    Url = "/Department/index",
            //    ParentId = null
            //},
             new SysMenu
             {
                 Id = baseSysMenuId,
                 Name = "系统管理",
                 Code = "baseSysMenu",
                 SerialNumber = 1,
                 Icon = "fa fa-link",
                 ParentId = null
             },
            new SysMenu
            {
                Name = "角色管理",
                Code = "Role_Index",
                SerialNumber = 1,
                Icon = "fa fa-link",
                Url = "/System/sysrole.html",
                ParentId = baseSysMenuId
            },

             new SysMenu
             {
                 Name = "用户管理",
                 Code = "UserController_Index",
                 SerialNumber = 2,
                 Icon = "fa fa-link",
                 ParentId = baseSysMenuId,
                 Url = "/System/SysUser.html"
             },
             new SysMenu
             {
                 Name = "功能管理",
                 Code = "Menu_Index",
                 SerialNumber = 3,
                 Icon = "fa fa-link",
                 ParentId = baseSysMenuId,
                 Url = "/System/sysmenulist.html"
             },
             new SysMenu
             {
                 Name = "字典管理",
                 Code = "Dict_Index",
                 SerialNumber = 4,
                 Icon = "fa fa-link",
                 ParentId = baseSysMenuId,
                 Url = "/System/SysDictList.html"
             },
             new SysMenu
             {
                 Name = "职务管理",
                 Code = "Duty_Index",
                 SerialNumber = 5,
                 Icon = "fa fa-link",
                 ParentId = baseSysMenuId,
                 Url = "/System/SysDutyList.html"
             },
              new SysMenu
              {
                  Name = "部门管理",
                  Code = "Department_Index",
                  SerialNumber = 6,
                  Icon = "fa fa-link",
                  ParentId = baseSysMenuId,
                  Url = "/System/SysDepartmentList.html",

              }

              );

            context.SaveChanges();
            #endregion

            #region 用户管理功能初始化
            var parentMenu = context.Set<SysMenu>().SingleOrDefault(m => m.Name == "用户管理");
            MenuInit(parentMenu.Id, context, nameof(UserController));
            context.SaveChanges();
            #endregion

            #region Menu
            parentMenu = context.Set<SysMenu>().SingleOrDefault(m => m.Name == "功能管理");
            MenuInit(parentMenu.Id, context, nameof(MenuController));
            context.SaveChanges();
            #endregion

            #region Role
            parentMenu = context.Set<SysMenu>().SingleOrDefault(m => m.Name == "角色管理");
            MenuInit(parentMenu.Id, context, nameof(RoleController));
            context.SaveChanges();
            #endregion


            #region Dict字典管理
            parentMenu = context.Set<SysMenu>().SingleOrDefault(m => m.Name == "字典管理");
            MenuInit(parentMenu.Id, context, nameof(DataDirController));
            context.SaveChanges();
            #endregion

            #region Duty职务管理
            parentMenu = context.Set<SysMenu>().SingleOrDefault(m => m.Name == "职务管理");
            MenuInit(parentMenu.Id, context, nameof(SysDutyController));
            context.SaveChanges();
            #endregion
            #region Department部门管理
            parentMenu = context.Set<SysMenu>().SingleOrDefault(m => m.Name == "部门管理");
            MenuInit(parentMenu.Id, context, nameof(SysDepartmentController));
            context.SaveChanges();
            #endregion
            #region UserPost用户部门职务管理
            //parentMenu = context.Set<SysMenu>().SingleOrDefault(m => m.Name == "用户部门职务管理");
            //MenuInit(parentMenu.Id, context, nameof(DataDirController));
            //context.SaveChanges();
            #endregion


            //角色
            context.Set<SysRole>().Add(new SysRole()
            {
                Code = "Admin",
                Name = "管理员",
                Remarks = "管理员",
                CreateTime = DateTime.Now,
                CreateFactory = headofficeId
            });

            context.SaveChanges();
            context.Set<SysUserRole>().Add(new SysUserRole
            {
                SysRoleId = context.Set<SysRole>().FirstOrDefault(r => r.Name == "管理员").Id,
                SysUserId = context.Set<SysUser>().FirstOrDefault(u => u.LoginName == "admin").Id
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "001",
                Key = "0",

                Value = "女"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "001",
                Key = "1",
                Value = "男"
            });

            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "112",
                Key = "1",
                Value = "了解知道"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "112",
                Key = "2",
                Value = "熟练掌握"
            });

            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "112",
                Key = "3",
                Value = "指导他人"
            });

            //承包商_考试_是否合格
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "57",
                Key = "1",
                Value = "合格"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "57",
                Key = "2",
                Value = "不合格"
            });


            //承包商_考试_难易程度

            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "55",
                Key = "1",
                Value = "简单"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "55",
                Key = "2",
                Value = "困难"
            });
            //承包商_违规管理_处理结果
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9021",
                Key = "1",
                Value = "不拉入黑名单"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9021",
                Key = "2",
                Value = "拉入黑名单"
            });

            //文件类型
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9030",
                Key = "1",
                Value = "txt"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9030",
                Key = "2",
                Value = "exe"
            });

            //培训方式
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "w64",
                Key = "0",
                Value = "线上"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "w64",
                Key = "1",
                Value = "线下"
            });
            //培训状态
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "w67",
                Key = "0",
                Value = "未开始"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "w67",
                Key = "1",
                Value = "已完成"
            });


            #region 隐患-整改状态6010
            //整改状态 隐患进度(/0待审批/1已生效/2审批退回/3验收通过/4验收退回)
            //整改状态 隐患进度(/待确认/待整改/整改中/待验收/验收通过/验收退回)
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "6010",
                Key = "0",
                Value = "待确认"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "6010",
                Key = "1",
                Value = "待整改"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "6010",
                Key = "2",
                Value = "整改中"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "6010",
                Key = "3",
                Value = "待验收"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "6010",
                Key = "4",
                Value = "验收通过"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "6010",
                Key = "5",
                Value = "验收退回"
            });
            #endregion
            #region 隐患-排查频次提醒类别6020
            //隐患-排查频次提醒类别(年月)
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "6020",
                Key = "4",
                Value = "自定义"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "6020",
                Key = "2",
                Value = "周"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "6020",
                Key = "3",
                Value = "日"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "6020",
                Key = "1",
                Value = "月"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "6020",
                Key = "0",
                Value = "年"
            });
            #endregion
            //隐患管理- 排查岗位  
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "60",
                Key = "0",
                Value = "维修"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "60",
                Key = "1",
                Value = "叉车"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "60",
                Key = "2",
                Value = "救生员"
            });
            #region 隐患-班组级别7835
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "7835",
                Key = "0",
                Value = "公司级"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "7835",
                Key = "1",
                Value = "班组级"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "7835",
                Key = "2",
                Value = "部门级"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "7835",
                Key = "3",
                Value = "车间级"
            });
            #endregion
            #region 隐患-排查标准类别1001002
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "1001002",
                Key = "0",
                Value = "知识库"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "1001002",
                Key = "1",
                Value = "基础库"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "1001002",
                Key = "2",
                Value = "手工"
            });
            #endregion
            #region 隐患-排查标准类型102
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "102",
                Key = "1",
                Value = "日常隐患排查"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "102",
                Key = "2",
                Value = "综合性隐患排查"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "102",
                Key = "3",
                Value = "节假日、季节性排查"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "102",
                Key = "4",
                Value = "专家诊断性排查"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "102",
                Key = "5",
                Value = "事故类比排查"
            });
            #endregion

            #region 人员的反应9040
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9040",
                Key = "0",
                Value = "调整个人防护装备"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9040",
                Key = "0",
                Value = "改变姿势"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9040",
                Key = "0",
                Value = "重新安排工作"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9040",
                Key = "0",
                Value = "停止安排工作"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9040",
                Key = "0",
                Value = "接地"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9040",
                Key = "0",
                Value = "上锁"
            });
            #endregion

            #region 个人防护装备9041
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9041",
                Key = "0",
                Value = "从头到脚的检查"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9041",
                Key = "0",
                Value = "头部"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9041",
                Key = "0",
                Value = "眼镜及脸部"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9041",
                Key = "0",
                Value = "耳朵"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9041",
                Key = "0",
                Value = "呼吸系统"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9041",
                Key = "0",
                Value = "胳膊和手"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9041",
                Key = "0",
                Value = "身躯"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9041",
                Key = "0",
                Value = "腿部及脚部"
            });
            #endregion

            #region 人员位子9042
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9042",
                Key = "0",
                Value = "撞到物体或被物体撞到"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9042",
                Key = "0",
                Value = "被夹在物品上部、内部或期间"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9042",
                Key = "0",
                Value = "跌倒"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9042",
                Key = "0",
                Value = "接触极低或极高的温度"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9042",
                Key = "0",
                Value = "接触电流"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9042",
                Key = "0",
                Value = "吸入、吸收或吞入危险的物质"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9042",
                Key = "0",
                Value = "重复性动作"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9042",
                Key = "0",
                Value = "别扭的姿势/僵硬的姿势"
            });
            #endregion

            #region 工具及设备9043
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9043",
                Key = "0",
                Value = "使用不合时宜的工具或设备"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9043",
                Key = "0",
                Value = "使用方法不正确"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9043",
                Key = "0",
                Value = "工具及设备不在良好状态"
            });

            #endregion

            #region 程序9044
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9044",
                Key = "0",
                Value = "可获得"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9044",
                Key = "0",
                Value = "充分"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9044",
                Key = "0",
                Value = "被知道"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9044",
                Key = "0",
                Value = "被理解"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9044",
                Key = "0",
                Value = "被遵循"
            });
            #endregion

            #region 秩序标准9045
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9045",
                Key = "0",
                Value = "被知道"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9045",
                Key = "0",
                Value = "被理解"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "9045",
                Key = "0",
                Value = "被遵循"
            });
            #endregion

            #region 班次 n51
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "n51",
                Key = "0",
                Value = "白"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "n51",
                Key = "1",
                Value = "中"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "n51",
                Key = "2",
                Value = "夜"
            });

            #endregion


            //隐患分类
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "100",
                Key = "0",
                Value = "隐患分类A"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "100",
                Key = "1",
                Value = "隐患分类B"
            });
            #region 区域
            var listFactoryList = new List<KeyValue<string, Guid>>() {
                new KeyValue<string, Guid>() {  Key="tl",Value=tlFactoryId},
                new KeyValue<string, Guid>() {  Key="sd",Value=sdFactoryId},
                new KeyValue<string, Guid>() {  Key="hy",Value=hyFactoryId}
                 };
            listFactoryList.ForEach(item =>
            {
                //区域
                context.Set<SysArea>().Add(new SysArea()
                {
                    SubFactory = item.Value,
                    Code = "q1",
                    Name = "区域1" + item.Key
                });
                context.Set<SysArea>().Add(new SysArea()
                {
                    SubFactory = item.Value,
                    Code = "q2",
                    Name = "区域2" + item.Key
                });
                context.Set<SysArea>().Add(new SysArea()
                {
                    SubFactory = item.Value,
                    Code = "q3",
                    Name = "区域3" + item.Key
                });
            });

            #endregion


            //隐患排查情况
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "115",
                Key = "0",
                Value = "正常"
            });

            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "115",
                Key = "1",
                Value = "异常"
            });
            context.Set<SysDataDic>().Add(new SysDataDic()
            {
                Category = "115",
                Key = "2",
                Value = "异常"
            });

            //承包商施工类别
            string valueListStr = "工程技术类、设备检（维）修类、操作运行类、建筑类、运输类、绿化工程类、物资供应类、广告宣传类、机加工类、机泵维修、设备内部清洗类";
            var valueList = valueListStr.Split("、");
            for (int i = 0; i < valueList.Count(); i++)
            {
                context.Set<SysDataDic>().Add(new SysDataDic()
                {
                    Category = "cbssglb",
                    Key = i.ToString(),
                    Value = valueList[i]
                });
            }

            context.SaveChanges();



            context.SysFactory.Add(new Entitys.Sys.SysFactory
            {
                Id = tlFactoryId,
                FactoryCode = "1",
                FactoryName = "铜陵圣奥工厂",
                FactoryType = "0"
            }); ;
            context.SysFactory.Add(new Entitys.Sys.SysFactory
            {
                Id = sdFactoryId,
                FactoryCode = "2",
                FactoryName = "山东圣奥工厂",
                FactoryType = "0"
            });
            context.SysFactory.Add(new Entitys.Sys.SysFactory
            {
                Id = hyFactoryId,
                FactoryCode = "3",
                FactoryName = "华阳药厂",
                FactoryType = "0"
            });
            context.SysFactory.Add(new Entitys.Sys.SysFactory
            {
                Id = headofficeId,
                FactoryCode = "4",
                FactoryName = "总部",
                FactoryType = "1"
            });
            context.SaveChanges();
            DbInitializerHelp.Initialize(context, _webAppOptions);


        }
        public static void Initialize(IWebHost host)
        {

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var context = services.GetRequiredService<CoreDBContext>();
                    var webAppOptions = services.GetRequiredService<IOptionsMonitor<WebAppOptions>>();
                    Initialize(context, webAppOptions.CurrentValue);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService<ILogger<DBInitializer>>();
                    logger.LogError(ex, "初始化数据库异常.");
                }
            }
        }
        public static void MenuInit(Guid? parentId, CoreDBContext coreDBContext, string fromName)
        {

            var type = ControllerReflectHelp.GetControllerType(fromName);
            PermissionHelp permissionHelp = new PermissionHelp(type);
            var list = permissionHelp.GetPermissionAttribute();
            foreach (var item in list)
            {
                coreDBContext.Set<SysMenu>().Add(new SysMenu
                {
                    Code = item.FunctionCode,
                    Name = item.FunctionName,
                    ParentId = parentId,
                    Type = 1
                });
            }
            coreDBContext.SaveChanges();
        }


    }


}
